package com.softwareconstruction.client;

import com.softwareconstruction.exception.NoEnoughPapersException;
import com.softwareconstruction.management.PaperList;
import com.softwareconstruction.paper.*;
import com.softwareconstruction.selection.PaperSelection;
import com.softwareconstruction.selection.StudentCountVisitor;
import com.softwareconstruction.selection.Visitor;
import com.softwareconstruction.student.Student;
import com.softwareconstruction.strategy.OrderSelectionStrategy;

import java.util.Arrays;
import java.util.List;

/**
 * 客户端程序，演示论文选择流程
 */
public class Client {
    public static void main(String[] args) {
        // 创建论文列表
        List<Paper> paperList = Arrays.asList(
                new Thesis("软件工程研究"),
                new ConferencePaper("人工智能应用"),
                new JournalPaper("计算机网络技术"),
                new JournalPaper("数据结构与算法"),
                new Thesis("机器学习基础")
        );

        PaperList list0fRainy = new PaperList("Rainy", paperList);

        // 创建学生列表
        List<Student> students = Arrays.asList(
                new Student("Cloudy1", 1, PaperType.Thesis),
                new Student("Cloudy2", 2, PaperType.Any),
                new Student("Cloudy3", 1, PaperType.JournalPaper),
                new Student("Cloudy4", 3, PaperType.Any)
        );

        // 使用多线程模拟学生同时选择论文
        for (Student student : students) {
            new Thread(() -> {
                try {
                    list0fRainy.selectPaperByRandom(student);
                    System.out.println(student.getName() + " 选择成功");
                } catch (NoEnoughPapersException e) {
                    System.err.println(e.getMessage());
                }
            }).start();
        }

        // 等待所有线程完成
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 输出选择结果
        PaperSelection selectionResult = list0fRainy.getSelectionResult();
        System.out.println("\n最终选择结果：");
        System.out.println(selectionResult);

        // 使用Visitor模式统计选择了论文的学生数量
        Visitor studentCountVisitor = new StudentCountVisitor();
        selectionResult.accept(studentCountVisitor);
        System.out.println("共有 " + ((StudentCountVisitor) studentCountVisitor).getCount() + " 名学生选择了论文");

        // 演示使用顺序选择策略
        System.out.println("\n演示使用顺序选择策略:");
        list0fRainy = new PaperList("Sunny", paperList);
        list0fRainy.setSelectionStrategy(new OrderSelectionStrategy());

        try {
            list0fRainy.selectPaperByRandom(new Student("Test", 2, PaperType.Any));
            System.out.println("顺序选择结果：");
            System.out.println(list0fRainy.getSelectionResult());
        } catch (NoEnoughPapersException e) {
            e.printStackTrace();
        }
    }
}